/**
 * 河北省通信建设有限公司信息化分公司 版权所有
 * <p/>
 * 文件:com.beautysport.common.ImageViewPager.java
 * <p/>
 * 创建时间:2013-3-29
 * <p/>
 * 创建人:宋志军
 */
package com.sxtyxt.online;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sxtyxt.adapter.BasePagerAdapter;
import com.sxtyxt.util.ImageLoaderUtil;
import com.sxtyxt.util.ToastUtil;
import com.sxtyxt.widget.photoview.HackyViewPager;
import com.sxtyxt.widget.photoview.PhotoView;
import com.sxtyxt.widget.photoview.PhotoViewAttacher;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import java.util.ArrayList;
import java.util.List;


/**
 * 大图查看器
 */
public class ImageViewPager extends Activity {

    private static final String TAG = "ImageViewPager";
    private HackyViewPager viewPager = null;
    private static List<String> imageList = new ArrayList<String>();
    private static int index = 0;
    private ImagePagerAdapter adapter = null;
    private TextView pageLabel;
    private ProgressBar pb = null;

    public static void setImageList(List<String> images) {
        ImageViewPager.imageList = images;
    }

    public static void setCurrentIndex(int index) {
        ImageViewPager.index = 0;
        ImageViewPager.index = index;
    }

   /* @Override
    protected int getLayoutId() {
        return R.layout.image_viewpager;
    }*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_viewpager);
        initUI();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(0, R.anim.fade_out);
    }

    //@Override
    public void initUI() {
        if (imageList != null && imageList.size() <= 0) {
            ToastUtil.getInstance().showToast(this,"加载大图失败");
            this.finish();
            return;
        }

        pb = (ProgressBar) findViewById(R.id.pb);
        viewPager = (HackyViewPager) findViewById(R.id.viewpager);
        pageLabel = (TextView) findViewById(R.id.pageLabel);
        //tvDesc = (TextView) findViewById(R.id.tv_des);
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                setPageInfo(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        adapter = new ImagePagerAdapter(this, R.layout.image_viewpager_item);
        adapter.setData(imageList);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(index, true);

        setPageInfo(index);
    }

    /**
     * 设置分页标签
     *
     * @param position
     */
    private void setPageInfo(int position) {
        Object[] obj = new Object[]{position + 1, imageList.size()};
        String info = String.format("%d/%d", obj);
        pageLabel.setText(info);
        // tvDesc.setText(imageList.get(position).getPhotoDes());
    }

    private class ImagePagerAdapter extends BasePagerAdapter<String> {

        public ImagePagerAdapter(Context context, int resouceId) {
            super(context, resouceId);
        }

        @Override
        protected void initPagerItem(View view, final String item, int position) {
            final PhotoView imageView = (PhotoView) view
                    .findViewById(R.id.image);
            final String imgUrl = item;
            ImageLoader.getInstance().displayImage(SystemConst.DEFAULT_IMAGE_URL + imgUrl, imageView, ImageLoaderUtil.getOptionCustom(R.drawable.img_default), new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    pb.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    pb.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    pb.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                }
            });
            imageView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {

                @Override
                public void onPhotoTap(View view, float x, float y) {
                    finish();
                }
            });

        }
    }
}
